package com.balance.anole.erp.report.script;

import java.awt.Color;
import java.util.ArrayList;

import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
import net.sf.jasperreports.renderers.JCommonDrawableRenderer;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class OrderAnalyzeViewProductTop extends JRDefaultScriptlet{
	
	public void afterReportInit() throws JRScriptletException{
		ArrayList<String[]> date = (ArrayList<String[]>)this.getParameterValue("paramForChart");
		
		JFreeChart jfreechart = ChartFactory.createBarChart("", //图形标题名称
				"",//domain 轴 Lable,这里先简单理解为横坐标Lable好了
				"", //range 轴 Lable,这里也先简单理解为纵坐标Lable好了
				createDataset(date), // dataset
				PlotOrientation.VERTICAL, //垂直显示
				false, // legend?
				false, // tooltips?
				true); //URLs?

		jfreechart.setBackgroundPaint(Color.white); //设定背景色为白色
		CategoryPlot categoryplot = jfreechart.getCategoryPlot(); //获得 plot：CategoryPlot
		categoryplot.setBackgroundPaint(Color.lightGray); //设定图表数据显示部分背景色
		categoryplot.setDomainGridlinesVisible(true); //可见
		categoryplot.setRangeGridlinePaint(Color.white); //纵坐标网格线白色
		categoryplot.setDomainGridlinePaint(Color.white); //横坐标网格线白色
		
		//下面两行使纵坐标的最小单位格为整数
		NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		BarRenderer barrenderer = (BarRenderer)categoryplot.getRenderer(); //获得renderer 注意这里是下嗍造型到BarRenderer！！
		
		barrenderer.setBaseOutlinePaint(Color.BLACK); // 设置Bar边框颜色
		//barrenderer.setDrawBarOutline(false); // Bar的外轮廓线不画
		barrenderer.setDrawBarOutline(true); 
		
		//GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue,0.0F, 0.0F, new Color(0, 0, 64)); //设定特定颜色	
		//barrenderer.setSeriesPaint(0, gradientpaint); //给series1 Bar设定上面定义的颜色
		barrenderer.setSeriesPaint(0,new Color(51, 204, 204));

		//CategoryAxis categoryaxis = categoryplot.getDomainAxis(); //横轴上的 Lable 45度倾斜
		//categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
	
		this.setVariableValue("Chart", new JCommonDrawableRenderer(jfreechart));
	}
	
	private CategoryDataset createDataset(ArrayList<String[]> paramForChart){
		String series = "";
		DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
		
		if(paramForChart!=null && paramForChart.size()>0){
			for(int i=0;i<paramForChart.size();i++){
				defaultcategorydataset.addValue(Double.parseDouble((paramForChart.get(i))[1]), series, (paramForChart.get(i))[0]);				
			}		
		}

		return defaultcategorydataset;
	} 
}
